RefuseLeaveRequestCommandHandler   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 29
dl 0
loc 34
c 0
b 0
f 0
rs 10

1 Function

Rating   Name   Duplication   Size   Complexity  
A execute 0 24 3
1
import { Inject } from '@nestjs/common';
2
import { CommandHandler } from '@nestjs/cqrs';
3
import { RefuseLeaveRequestCommand } from './RefuseLeaveRequestCommand';
4
import { ILeaveRequestRepository } from 'src/Domain/HumanResource/Leave/Repository/ILeaveRequestRepository';
5
import { LeaveRequestNotFoundException } from 'src/Domain/HumanResource/Leave/Exception/LeaveRequestNotFoundException';
6
import { IDateUtils } from 'src/Application/IDateUtils';
7
import { LeaveRequestCantBeModeratedException } from 'src/Domain/HumanResource/Leave/Exception/LeaveRequestCantBeModeratedException';
8
import { CanLeaveRequestBeModerated } from 'src/Domain/HumanResource/Leave/Specification/CanLeaveRequestBeModerated';
9
10
@CommandHandler(RefuseLeaveRequestCommand)
11
export class RefuseLeaveRequestCommandHandler {
12
  constructor(
13
    @Inject('ILeaveRequestRepository')
14
    private readonly leaveRequestRepository: ILeaveRequestRepository,
15
    @Inject('IDateUtils')
16
    private readonly dateUtils: IDateUtils,
17
    private readonly canLeaveRequestBeModerated: CanLeaveRequestBeModerated
18
  ) {}
19
20
  public async execute(command: RefuseLeaveRequestCommand): Promise<string> {
21
    const { moderator, id, moderationComment } = command;
22
23
    const leaveRequest = await this.leaveRequestRepository.findOneById(id);
24
    if (!leaveRequest) {
25
      throw new LeaveRequestNotFoundException();
26
    }
27
28
    if (
29
      false ===
30
      this.canLeaveRequestBeModerated.isSatisfiedBy(leaveRequest, moderator)
31
    ) {
32
      throw new LeaveRequestCantBeModeratedException();
33
    }
34
35
    leaveRequest.refuse(
36
      moderator,
37
      this.dateUtils.getCurrentDateToISOString(),
38
      moderationComment
39
    );
40
    await this.leaveRequestRepository.save(leaveRequest);
41
42
    return leaveRequest.getId();
43
  }
44
}
45